package com.mofang.powerdekorhelper.activity.news;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.adapter.NewsInfoAdapter;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewAdapter;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.NewsInfo;
import com.mofang.powerdekorhelper.persenter.NewsInfoPersenter;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.NewsInfoView;
import com.mofang.powerdekorhelper.witget.MySwipeRefreshLayout;
import com.mofang.powerdekorhelper.witget.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by luofei on 2017/6/29 0029.
 */
public class NewsInforActivity extends MvpActivity<NewsInfoView, NewsInfoPersenter> implements NewsInfoView,
        SwipeRefreshLayout.OnRefreshListener, BaseRecycleViewList.MyRecycleOnScrollListener {

    @BindView(R.id.news_infomation_title)
    TitleBar mTitleBar;

    @BindView(R.id.news_infomation_recycle)
    BaseRecycleViewList newsRecycleView;

    @BindView(R.id.news_infomation_refresh)
    MySwipeRefreshLayout refreshLayout;

    @BindView(R.id.news_infomation_null_imv)
    ImageView nullImv;

    private List<NewsInfo.Data> newsList;

    private int page = 1;
    private int pageSize = 10;

    private boolean isLoadAll;

    NewsInfoAdapter adapter;

    private LoadPrograss loadDialog;

    @Override
    public void initView() {

        inflateLayout(R.layout.activity_news_infomation_layout);
        initTitleBarWithback(mTitleBar, R.string.news_infomation_title);
        loadDialog = new LoadPrograss(this);
        refreshLayout.setOnRefreshListener(this);
        newsRecycleView.setListOnScrollListener(this);
    }

    @Override
    public void initData() {
        newsList = new ArrayList<>();
        presenter.getNewsInformation(page, pageSize);
    }

    @Override
    public void onRefresh() {
        page = 1;
        presenter.getNewsInformation(page, pageSize);
    }

    @Override
    public void loadMore() {
        if (!isLoadAll) {
            presenter.getNewsInformation(++page, pageSize);
        }
    }

    @Override
    public void setNewsInfomation(NewsInfo newsInfomation) {

        if (newsInfomation.getResult().getData().size() > 0) {

            if (page == 1) {

                adapter = new NewsInfoAdapter(newsInfomation.getResult().getData(), this, R.layout.news_info_list_item_layout);

                adapter.setOnItemClickListner(new BaseRecycleViewAdapter.OnItemClickListner() {
                    @Override
                    public void onItemClickListner(ViewGroup parent, View v, int position) {

                        NewsInfo.Data data = (NewsInfo.Data) v.getTag();

                        startActivity(new Intent(NewsInforActivity.this, NewsInfoDetialActivity.class)
                                .putExtra("ArticleId", data.getId()));
                    }
                });

                newsRecycleView.setAdapter(adapter);
            } else {
                adapter.addData(newsInfomation.getResult().getData());
            }
        } else {
            if (page == 1) {
                nullImv.setVisibility(View.VISIBLE);
            } else {
                isLoadAll = true;
            }
            toast("暂无更多资讯");
        }

        hideProgress();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public NewsInfoPersenter initPresenter() {
        return new NewsInfoPersenter();
    }

    @Override
    public void showProgress() {
        loadDialog.show();
    }

    @Override
    public void hideProgress() {
        loadDialog.hide();
        refreshLayout.hide();
    }

    @Override
    public void toast(CharSequence s) {

        T.showShort(NewsInforActivity.this, s);
    }

    @Override
    public void toast(int id) {

        T.showShort(NewsInforActivity.this, getResources().getString(id));
    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {
        hideProgress();
    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {
        loadDialog.hide();
        refreshLayout.hide();
    }
}
